package srjava.messages;

import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedList;

public class AliveRet extends Message {
	LinkedList<ServerInfo> serverList;
	LinkedList<Task> taskList;
	LinkedList<TaskInfo> doneTasks;
	
	public LinkedList<ServerInfo> getServerList() {
		return serverList;
	}

	public void setServerList(LinkedList<ServerInfo> serverList) {
		this.serverList = serverList;
	}

	public LinkedList<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(LinkedList<Task> taskList) {
		this.taskList = taskList;
	}
	
	public AliveRet() {
		this.msgType = MessageType.ALIVE_RET;
		this.serverList = new LinkedList<ServerInfo>();
		this.taskList = new LinkedList<Task>();
		this.doneTasks = new LinkedList<TaskInfo>();
	}

	public LinkedList<TaskInfo> getDoneTasks() {
		return doneTasks;
	}

	public void setDoneTasks(LinkedList<TaskInfo> doneTasks) {
		this.doneTasks = doneTasks;
	}

	public AliveRet(LinkedList<ServerInfo> serverList, LinkedList<Task> taskList) {
		this.msgType = MessageType.ALIVE_RET;
		this.serverList = serverList;
		this.taskList = taskList;
	}

	@Override
	public String serialize() {
		String pom = super.serialize() + serverList.size() + " ";
		Iterator<ServerInfo> it = serverList.iterator();
		while (it.hasNext()) {
			pom += it.next().serialize();
		}
		pom += taskList.size() + " ";
		Iterator<Task> it2 = taskList.iterator();
		while (it2.hasNext()) {
			pom += it2.next().serialize();
		}
		pom += doneTasks.size() + " ";
		Iterator<TaskInfo> it3 = doneTasks.iterator();
		while (it3.hasNext()) {
			pom += it3.next().serialize();
		}
		return pom;
	}

	@Override
	public void deSerialize(StringReader s) throws IOException {
		int size = IntString.parseInt(s);
		for(int i=0; i<size; i++)
		{
			ServerInfo pom = new ServerInfo();
			pom.deSerialize(s);
			serverList.add(pom);
		}
		size = IntString.parseInt(s);
		for(int i=0; i<size; i++)
		{
			Task pomT = new Task();
			pomT.deSerialize(s);
			taskList.add(pomT);
		}
		size = IntString.parseInt(s);
		for(int i=0; i<size; i++)
		{
			TaskInfo pomT = new TaskInfo();
			pomT.deSerialize(s);
			doneTasks.add(pomT);
		}
	}
	
	
}
